#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    time.setInterval(500);
    time.setSingleShot(false);
    connect(&time, SIGNAL(timeout()),this,SLOT(ProceedSendMessage()));
    connect(ClientApplication::Instance(),SIGNAL(roomsList()),this,SLOT(ProceedRoomsList()));
    connect(ClientApplication::Instance(),SIGNAL(fail(QString)),this,SLOT(Failed(QString)));
    connect(ClientApplication::Instance(),SIGNAL(joinRoom()),this,SLOT(ProceedJoinRoom()));
    connect(ClientApplication::Instance(),SIGNAL(getUsers()),this,SLOT(ProceedGetUsers()));
    connect(ClientApplication::Instance(),SIGNAL(sendMessage()),this,SLOT(ProceedSendMessage()));
    connect(ClientApplication::Instance(),SIGNAL(getMessages()),this,SLOT(ProceedGetMessages()));
    QueryManager::SendRoomsList(*ClientApplication::Instance()->getSession());
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::ProceedRoomsList()
{
    for (int i=0; i<ClientApplication::Instance()->getSession()->getRoomCount(); i++)
    {
        if (ClientApplication::Instance()->getSession()->getRoom(i)->getNeed())
            this->ui->cBRoomsList->addItem(ClientApplication::Instance()->getSession()->getRoom(i)->getName());
        else
            this->ui->cBRoomsList->addItem(ClientApplication::Instance()->getSession()->getRoom(i)->getName());
    }
}

void MainWindow::ProceedJoinRoom()
{
    for (int i=0; i<ClientApplication::Instance()->getSession()->getRoomCount();i++)
    {
        if (ClientApplication::Instance()->getSession()->getRoom(i)->getName() == this->ui->cBRoomsList->currentText())
        {
            ClientApplication::Instance()->getSession()->setCurrenRoom(ClientApplication::Instance()->getSession()->getRoom(i));
        }
    }
    time.start();
    /*this->ui->cBRoomsList->clear();
    ClientApplication::Instance()->getSession()->cleanRooms();
    QueryManager::SendRoomsList(*ClientApplication::Instance()->getSession());
    this->ui->cBRoomsList->setEditText(ClientApplication::Instance()->getSession()->getCurrentRoom()->getName());
    */QueryManager::SendGetUsers(*ClientApplication::Instance()->getSession(),ClientApplication::Instance()->getSession()->getCurrentRoom()->getName());
}

void MainWindow::on_pBJoinRoom_clicked()
{
    QueryManager::SendJoinRoom(*ClientApplication::Instance()->getSession(),this->ui->cBRoomsList->currentText(),"");
    //todo password id
}

void MainWindow::ProceedGetUsers()
{
    foreach (User* u, ClientApplication::Instance()->getSession()->getCurrentRoom()->getUsers())
    {
        this->ui->lWUsers->addItem(u->getLogin());
    }
}

void MainWindow::on_pBSend_clicked()
{
    QueryManager::SendMessage(*ClientApplication::Instance()->getSession(),ClientApplication::Instance()->getSession()->getCurrentRoom()->getName(),this->ui->lETypeString->text());
    this->ui->lETypeString->clear();
}

void MainWindow::ProceedSendMessage()
{
    if (ClientApplication::Instance()->getSession()->getCurrentRoom() != NULL)
        QueryManager::SendGetMessages(*ClientApplication::Instance()->getSession(),ClientApplication::Instance()->getSession()->getCurrentRoom()->getName());
}

void MainWindow::ProceedGetMessages()
{
    this->ui->tEChat->clear();
    for (int i=0; i<ClientApplication::Instance()->getSession()->getCurrentRoom()->msgsCount();i++)
    {
        this->ui->tEChat->addItem(ClientApplication::Instance()->getSession()->getCurrentRoom()->getMsg(i));
    }
}

void MainWindow::on_cBRoomsList_currentIndexChanged(const QString &arg1)
{

}

void MainWindow::Failed(QString cause)
{
    QMessageBox msgBox;
    msgBox.setText(ErrorDict::getMessage(cause));
    msgBox.setModal(true);
    msgBox.exec();
}
